package com.hq.zb.exception;

import com.hq.zb.utils.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;

/**
 * 统一捕获异常的
 * @author huanghanrui
 * @date 2022-03-24
 */
@ControllerAdvice
public class BizExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(BizExceptionHandler.class);

    @ExceptionHandler({Exception.class})
    public ResponseEntity<Result> handleException(Exception exception, HttpServletRequest request) {
        final boolean isBizException = exception instanceof BizException;
        logger.error("触发请求:[{}]时系统出现异常，异常类型：{}", request.getRequestURI(), isBizException ? "业务异常" : "系统异常");
        BizException bizException = (BizException) exception;
        Result result = new Result(bizException);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }
}

